package com.zytlifeservice.config;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by MJS on 2017/7/13.
 */
@Component
public class QianmiConfig {
    private final Logger logger = LoggerFactory.getLogger(QianmiConfig.class);
    private String refreshToken;
    private String accessToken;
    private String bm_callback_url;
    private String bm_api_url;
    private String bm_msg_url;
    private String app_secret;
    private String app_key;
    private PropertiesConfiguration configuration;

    private String bm_properties_path;

    private QianmiConfig() {
    }

    @Value("${bm_properties_path}")
    public synchronized void init(String bm_properties_path) {
        this.bm_properties_path = bm_properties_path;
        logger.info("properties path={}", bm_properties_path);
        if (configuration == null) {
            try {
                configuration = new PropertiesConfiguration(bm_properties_path);
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
            if (configuration == null) {
                throw new IllegalStateException("can't find file by path");
            } else {
                refreshToken = configuration.getString("refreshToken");
                accessToken = configuration.getString("accessToken");
                bm_callback_url = configuration.getString("bm_callback_url");
                bm_api_url = configuration.getString("bm_api_url");
                bm_msg_url = configuration.getString("bm_msg_url");
                app_secret = configuration.getString("app_secret");
                app_key = configuration.getString("app_key");
                logger.info("千米配置文件名=" + bm_properties_path);
                logger.info(getDescription());
            }
        }
    }

    private String getDescription() {
        StringBuilder sb = new StringBuilder("\n");
        sb.append("--refreshToken=").append(getRefreshToken()).append("\n");
        sb.append(", accessToken=").append(getAccessToken()).append("\n");
        sb.append(", bm_callback_url=").append(getBm_callback_url()).append("\n");
        sb.append(", bm_api_url=").append(getBm_api_url()).append("\n");
        sb.append(", bm_msg_url=").append(getBm_msg_url()).append("\n");
        sb.append(", app_secret=").append(getApp_secret()).append("\n");
        sb.append(", app_key=").append(getApp_key()).append("\n");
        return sb.toString();
    }

    public synchronized void setProperty(String key, String value) throws ConfigurationException, FileNotFoundException {
        if (configuration != null) {
            logger.info("key=" + key + ",value=" + value);
            configuration.setProperty(key, value);
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            configuration.setHeader("update time = " + time);
            configuration.save(new FileOutputStream(bm_properties_path));
        }
    }

    public void updateRefreshToken(String refreshToken) {
        try {
            setProperty("refreshToken", refreshToken);
            setRefreshToken(configuration.getString("refreshToken"));
        } catch (ConfigurationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public synchronized void updateAccessToken(String accessToken) {
        try {
            setProperty("accessToken", accessToken);
            setAccessToken(configuration.getString("accessToken"));
        } catch (ConfigurationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public String getBm_callback_url() {
        return bm_callback_url;
    }

    public String getBm_api_url() {
        return bm_api_url;
    }

    public String getBm_msg_url() {
        return bm_msg_url;
    }

    public String getApp_secret() {
        return app_secret;
    }

    public String getApp_key() {
        return app_key;
    }

    public void setBm_callback_url(String bm_callback_url) {
        this.bm_callback_url = bm_callback_url;
    }

    public void setBm_api_url(String bm_api_url) {
        this.bm_api_url = bm_api_url;
    }

    public void setBm_msg_url(String bm_msg_url) {
        this.bm_msg_url = bm_msg_url;
    }

    public void setApp_secret(String app_secret) {
        this.app_secret = app_secret;
    }

    public void setApp_key(String app_key) {
        this.app_key = app_key;
    }

    public void setConfiguration(PropertiesConfiguration configuration) {
        this.configuration = configuration;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }
}
